import { _decorator, AssetManager, Component, director, JsonAsset, Label, ProgressBar } from 'cc';
import CharacterManageSingle from '../character/CharacteManageSingle';
import SkillManageSingle from '../character/SkillManageSingle';
import LevelManageSingle from '../game/LevelManageSingle';
import { GameRes } from '../GameRes';
import ResourceManageSingleton from '../libs/utils/ResourceManageSingleton';
const { ccclass, property } = _decorator;

@ccclass('LoadingScene')
export class LoadingScene extends Component {
    /****************************************************************************************************************
     *Readonly 常量
     ****************************************************************************************************************/
    /****************************************************************************************************************
     *Property 定义变量
     ****************************************************************************************************************/
    /***************************************************************
     *PropertyUI 引用的控件
     ***************************************************************/
    @property({ group: { name: 'UI', id: '1' }, type: ProgressBar, tooltip: `加载进度文本` })
    private loadProgress: ProgressBar = undefined!;

    @property({ group: { name: 'UI', id: '1' }, type: Label, tooltip: `文本组件` })
    private loadLabel: Label = undefined!;
    /****************************************************************************************************************
     * Lifecycle 生命周期相关方法
     ****************************************************************************************************************/
    protected onLoad(): void {
        this.init();
    }

    protected onEnable(): void {

    }
    /****************************************************************************************************************
     * Public 公有方法
     ****************************************************************************************************************/
    /**
     * 初始化
     */
    private init() {
        let promise = Promise.all([this.loadConfig()]);
        promise.then(
            (value) => {
                console.log(value);
                this.loadLabel.string = `加载配置文件成功`;
                this.initComplete();
                director.loadScene(GameRes.GameScene.MENU)
            }, (error) => {
                this.loadLabel.string = `加载配置文件失败:${JSON.stringify(error)}`;
            }
        )
    }


    /**
     * 初始化配置数据
     */
    private initComplete() {
        LevelManageSingle.single.init();
        CharacterManageSingle.single.init();
        SkillManageSingle.single.init();
    }
    /****************************************************************************************************************
     * private 私有方法
     ****************************************************************************************************************/
    /**
     * 加载配置文件
     * @returns 
     */
    private loadConfig(): Promise<JsonAsset[]> {
        let promise = ResourceManageSingleton.singleton.loadJsonDir(`config/`,
            (finished: number, total: number, item: AssetManager.RequestItem) => {
                this.loadProgress.progress = finished / total;
                this.loadLabel.string = `正在加载配置文件${finished}/${total}:${item.info}`;
            });
        return promise;
    }
    /****************************************************************************************************************
     * Property Get && Set 属性的 get && set 函数
     ****************************************************************************************************************/
}


